package com.example.physicalexaminationsystem.service;

import com.example.physicalexaminationsystem.mapper.DepartmentDao;
import com.example.physicalexaminationsystem.model.DepartmentEntity;
import com.example.physicalexaminationsystem.vo.DepartmentVO;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author shiwenzhuo
 * 
 * @description
 */
@Component
public class DepartmentService {
    @Resource
    private DepartmentDao departmentDao;

    //添加科室信息
    public void addDepartment(Map<String, Object> params) {
        String name = params.get("name").toString();
        String info = params.get("info").toString();
        DepartmentEntity departmentEntity = new DepartmentEntity();
        departmentEntity.setName(name);
        departmentEntity.setInfo(info);
        //这里默认生效  后续需要使用需修改
        departmentEntity.setStatus(1);
        departmentDao.insert(departmentEntity);
    }

    //获取科室信息
    public List<DepartmentVO> getDepartmentList() {
        return departmentDao.selectList(null).stream().map(this::DepartmentEntityToDepartmentVO).collect(Collectors.toList());
    }

    //departmentEntity转departmentVO
    public DepartmentVO DepartmentEntityToDepartmentVO(DepartmentEntity departmentEntity) {
        DepartmentVO departmentVO = new DepartmentVO();
        departmentVO.setId(departmentEntity.getId());
        departmentVO.setName(departmentEntity.getName());
        departmentVO.setInfo(departmentEntity.getInfo());
        departmentVO.setStatus(departmentEntity.getStatus());
        return departmentVO;
    }
    //删除科室信息
    public void deleteDepartmentById(Long id) {
        departmentDao.deleteById(id);
    }
    //修改科室信息
    public void updateDepartmentById(Map<String, Object> params) {
        Long id = Long.valueOf(params.get("id").toString());
        String name = params.get("name").toString();
        String info = params.get("info").toString();
        Integer status = Integer.valueOf(params.get("status").toString());
        DepartmentEntity departmentEntity = new DepartmentEntity();
        departmentEntity.setId(id);
        departmentEntity.setName(name);
        departmentEntity.setInfo(info);
        departmentEntity.setStatus(status);
        departmentDao.updateById(departmentEntity);
    }
}
